package com.autumn.security.crypto;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import com.autumn.security.user.IAutumnUser;
import com.autumn.util.StringUtils;
import com.autumn.util.security.Md5Utils;

/**
 * 采用Md5密码驱动
 * 
 * @author 老码农 2018-04-14 18:03:25
 */
public class AutumnMd5PasswordProvider implements IAutumnPasswordProvider {

	private Charset charset = StandardCharsets.UTF_8;

	@Override
	public final String encode(IAutumnUser user, String rawPassword) {
		String source = String.format("%s_%s_%s", user.getId(), rawPassword, StringUtils.reverseOrder(rawPassword));
		return Md5Utils.md5(source, this.getCharset());

	}

	@Override
	public final boolean matches(IAutumnUser user, String rawPassword) {
		return this.encode(user, rawPassword).equals(user.getPassword());
	}

	/**
	 * 获取编辑
	 * 
	 * @return
	 */
	public Charset getCharset() {
		return charset;
	}

	/**
	 * 设置编码
	 * 
	 * @param charset 编码
	 */
	public void setCharset(Charset charset) {
		this.charset = charset;
	}

}
